<template>
  <div class="main-app">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="600px"
      style="margin-top: 50px"
    >
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status" :disabled="isView">
          <el-radio
            v-for="(item, index) in options"
            :key="index"
            :label="item.value"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容:" prop="content" style="width: 60%">
        <el-input v-model="form.content" type="textarea" :rows="4" clearable :disabled="isView" />
      </el-form-item>
    </el-form>
    <el-row style="margin-top: 40px; text-align: center">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" :disabled="isView" @click="save">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import { EquipmentStatus } from "../../../utils/resource";
import { saveTerm, getTermById } from "../../../api/system";
export default {
  data() {
    return {
      form: {
        content: "",
        status: "0",
        number: "",
      },
      fileList: [],
      options: EquipmentStatus,
      rules: {
        number: [
          {
            required: true,
            message: "请输入序号",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
        enclosure: [
          {
            required: true,
            message: "请上传PDF文件",
            trigger: "change",
          },
        ],
      },
      id: "",
      pdf: "",
      dialogVisible: false,
    };
  },
  computed: {
    isView() {
      // return this.$route.name === "dangerouswords-view";
      return this.$route.name === "危险用语详情";
    },
  },
  async created() {
    const { id } = this.$route.params;
    await this.getTermById(id);
    this.id = id;
  },
  methods: {
    async getTermById(id) {
      const { data } = await getTermById(id);
      this.form.content = data.content;
      this.form.status = data.status.toString();
    },
    save() {
      this.$refs["form"].validate(async(valid) => {
        if (valid) {
          let data = {
            content: this.form.content,
            status: this.form.status.toString(),
          };
          let message = "";
          if (!this.id) {
            message = "危险用语新增成功";
          } else {
            data = Object.assign(
              {
                id: this.id,
              },
              data
            );
            message = "危险用语修改成功";
          }
          const { success, msg } = await saveTerm(data);
          if (success) {
            this.$notify({
              title: "成功",
              message: message,
              type: "success",
              duration: 3000,
            });
            this.$router.push("/system/dangerouswords");
          } else {
            this.$notify.error({
              title: "失败",
              message: msg,
              duration: 3000,
            });
          }
        }
      });
    },
    cancel() {
      this.$router.push("/system/dangerouswords");
    },
  },
};
</script>
